const path = require("path");
const {
  sortDependencies,
  installDependencies,
  printMessage,
} = require("./utils");

module.exports = {
  helpers: {
    "raw-helper": function (options) {
      return options.fn();
    },
  },
  prompts: {
    name: {
      type: "string",
      required: true,
      message: "请输入项目名",
      default: "默认项目名称",
    },
    description: {
      type: "string",
      required: true,
      label: "请输入项目描述",
      default: "默认项目描述",
    },
    author: {
      type: "string",
      label: "作者",
    },
    // components: {
    //   type: "checkbox",
    //   message: "多选组件",
    //   name: "components",
    //   default: ["background", "contentScript", "popupTab"],
    //   choices: [
    //     { name: "background", value: "background", short: "background" },
    //     {
    //       name: "content script",
    //       value: "contentScript",
    //       short: "content script",
    //     },
    //     { name: "popup and tab", value: "popupTab" },
    //     { name: "option page", value: "optionPage", short: "option page" },
    //     { name: "locales", value: "locales", short: "locales" },
    //     { name: "devtool", value: "devtool", short: "devtool" },
    //   ],
    // },
    autoInstall: {
      type: "list",
      message: "选择一个安装工具? (推荐pnpm)",
      choices: [
        { name: "Yes, use NPM", value: "npm", short: "npm" },
        { name: "Yes, use YARN", value: "yarn", short: "yarn" },
        { name: "Yes, use PNPM", value: "pnpm", short: "pnpm" },
        { name: "No, I will handle that myself", value: false, short: "no" },
      ],
    },
  },
  filters: {
    "node_modules/**": "false",
    "dist/**": "false",
    /** 添加其它需过滤的文件 **/
    "package-lock.json": 'autoInstall === "npm"',
    "yarn.lock": 'autoInstall === "yarn"',
    "pnpm-lock.yaml": 'autoInstall === "pnpm"',
  },
  complete: function (data, { chalk }) {
    const green = chalk.green;

    sortDependencies(data, green);

    const cwd = path.join(process.cwd(), data.inPlace ? "" : data.destDirName);

    if (data.autoInstall) {
      installDependencies(cwd, data.autoInstall, green)
        .then(() => {
          printMessage(data, green);
        })
        .catch((e) => {
          console.log(chalk.red("Error:"), e);
        });
    } else {
      printMessage(data, chalk);
    }
  },
};
